//
//  NewFeatureViewController.swift
//  weibo
//
//  Created by cmh on 15/9/4.
//  Copyright © 2015年 cmh. All rights reserved.
//

import UIKit

private let cellCount = 4

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {
    
    init() {
        super.init(collectionViewLayout: UICollectionViewFlowLayout())
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        setLayOut()

    }
    private func setLayOut() {
    
        let layOut = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        
        layOut.itemSize = view.bounds.size
        layOut.minimumInteritemSpacing = 0
        layOut.minimumLineSpacing = 0
        layOut.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
    }
    
    // 实现数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        
        return cellCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.iamgeIndex = indexPath.item
    
        return cell
    }
    
    
    // 在collectionView滚动结束的时候调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let path = collectionView.indexPathsForVisibleItems().last!

        if path.item == cellCount - 1 {
        
            
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell

            cell.startBtnAnimation()
        }
    }

}



private class NewFeatureCell: UICollectionViewCell {

    // 第几张图片
    private var iamgeIndex: Int = 0 {
        didSet{
        
            iconView.image = UIImage(named: "new_feature_\(iamgeIndex+1)")
            
            startBtn.hidden = true
        
        }
    }
    
    // frame的大小
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setImage()
        
    }

    // 如果是有xib或sb会调用的方法
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setImage()
    
    }
    
    // 设置界面元素
    private  func setImage() {
        addSubview(iconView)
        addSubview(startBtn)
        
        // 制定布局
        iconView.frame = bounds
        
        // 开始按钮的布局
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
    }
    
    // 开始按钮的点击
    @objc private func startBtnClick() {
    
        // 发送切换控制器的通知
        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewContrillerNotification, object: nil)
    
    }
    
    // 开始按钮的动画
    private func startBtnAnimation() {
        
        startBtn.hidden = false
        
        // 按钮起始放大
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        // 动画
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
        }

    }
    
    
    // 懒加载图片控件
    private lazy var iconView = UIImageView()
    
    // 懒加载开始按钮控件
    private lazy var startBtn: UIButton = {
    
        let button = UIButton()
        
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        
        // 监听点击
        button.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
    
        return button
    }()
    
}
